package com.bawei.module_service.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_service.databinding.ServiceitemBinding
import com.bawei.module_service.model.ServiceItemModel

class ServiceAdapter: RecyclerView.Adapter<ServiceAdapter.ServiceViewHolder>() {

    private val list= mutableListOf<ServiceItemModel>()
    interface OnItemClickListener{
        fun clickListener(item: ServiceItemModel, position: Int)
    }
    var itemClickLister: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickLister=listener
    }
    fun setData(data:List<ServiceItemModel>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    //  创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServiceViewHolder {
        val binding=ServiceitemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )

        return ServiceViewHolder(binding)
    }

    override fun getItemCount(): Int {
        Log.d("GZY", "getItemCount: "+list.size)
        return list.size
    }

    override fun onBindViewHolder(holder: ServiceViewHolder, position: Int) {
        val binding=DataBindingUtil.getBinding<ServiceitemBinding>(holder.itemView)?:return
        binding.serviceI1.setImageResource(list[position].img)
        binding.serviceT1.text=list[position].title

        holder.itemView.setOnClickListener{
            itemClickLister?.clickListener(list[position],position)
        }

    }
    class ServiceViewHolder(val binding:ServiceitemBinding):RecyclerView.ViewHolder(binding.root)
}